package com.lin.fragments;

import com.lin.activity.AddFriendActivity;
import com.lin.activity.EntrepriseNearByActivity;
import com.lin.activity.FriendsShareDynamicActivity;
import com.lin.activity.FriendsShareWallActivity;
import com.lin.activity.SuperFriendsNearByActivity;
import com.lin.utils.Parameters;
import com.lin.views.CustomPopupWindow;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.SuperSchoolActivity;
import com.yunfeng.supercall.bean.Constants;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ExploreFragment extends Fragment implements OnClickListener, Constants{
	View mView;
	private LinearLayout mLinearLayoutSuperFriend;
	private LinearLayout mLinearLayoutSuperLive;
	private LinearLayout mLinearLayoutSuperChool;
	private LinearLayout mLinearLayoutNeighbour;
	
	
	CustomPopupWindow pw;
	private FrameLayout mFrameLayoutMore;
	private View frameLayoutSearch;
	DisplayImageOptions options1;
	DisplayImageOptions options2;
	DisplayImageOptions options3;
	DisplayImageOptions options4;
	private LinearLayout mLinearLayoutNeighbourEntreprise;
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
//		mView = inflater.inflate(R.layout.fragment_search, null);
		
		
	    Log.i("TAG", "onCreateView");

	    if (mView == null)
	    {
	    	mView = inflater.inflate(R.layout.fragment_search, null);
	    }
	    // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
	    ViewGroup parent = (ViewGroup) mView.getParent();
	    if (parent != null)
	    {
	      parent.removeView(mView);
	    }
		
	    frameLayoutSearch = mView.findViewById(R.id.framelayout_search);
	    		
	    ImageView imageview1 = (ImageView) mView.findViewById(R.id.imageview1);
	    ImageView imageview2 = (ImageView) mView.findViewById(R.id.imageview2);
	    ImageView imageview3 = (ImageView) mView.findViewById(R.id.imageview3);
	    ImageView imageview4 = (ImageView) mView.findViewById(R.id.imageview4);
	    ImageView imageview5 = (ImageView) mView.findViewById(R.id.imageview5);
		options1 = new DisplayImageOptions.Builder()
		.displayer(new RoundedBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.corners_avatar)))
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();
		
		options2 = new DisplayImageOptions.Builder()
		.displayer(new RoundedBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.corners_avatar)))
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();

		options3 = new DisplayImageOptions.Builder()
		.displayer(new RoundedBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.corners_avatar)))
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();
		
		options4 = new DisplayImageOptions.Builder()
		.displayer(new RoundedBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.corners_avatar)))
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();

//	    imageview1.setImageResource(R.drawable.ic_superfriend_wall);
		ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ic_superfriend_wall, imageview1, options1);
		ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ic_superman_dynamic, imageview2, options2);
		ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ic_superschool, imageview3, options3);
		ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ic_nearby, imageview4, options4);
		ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ic_entreprise_nearby, imageview5, options4);
		
	    pw = new CustomPopupWindow(getActivity());
	    mFrameLayoutMore = (FrameLayout) mView.findViewById(R.id.framelayout_more);
		mFrameLayoutMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
					pw.show();
			}
		});
		mLinearLayoutSuperFriend = (LinearLayout) mView.findViewById(R.id.super_friends);
		mLinearLayoutSuperLive = (LinearLayout) mView.findViewById(R.id.super_live);
		mLinearLayoutSuperChool = (LinearLayout) mView.findViewById(R.id.super_school);
		mLinearLayoutNeighbour = (LinearLayout) mView.findViewById(R.id.neighbour);
		mLinearLayoutNeighbourEntreprise = (LinearLayout) mView.findViewById(R.id.neighbour_entreprise);
		mLinearLayoutSuperFriend.setOnClickListener(this);
		mLinearLayoutSuperLive.setOnClickListener(this);
		mLinearLayoutSuperChool.setOnClickListener(this);
		mLinearLayoutNeighbour.setOnClickListener(this);
		frameLayoutSearch.setOnClickListener(this);
		mLinearLayoutNeighbourEntreprise.setOnClickListener(this);
		return mView;
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == mLinearLayoutSuperFriend) {
			startActivity(new Intent(getActivity(), FriendsShareWallActivity.class));
		} else if(v == mLinearLayoutSuperLive) {
			startActivity(new Intent(getActivity(),
					FriendsShareDynamicActivity.class));			
		} else if(v == mLinearLayoutSuperChool) {
			startActivity(new Intent(getActivity(),
					SuperSchoolActivity.class).putExtra(KEY_DATA, -1));			
		} else if(v == mLinearLayoutNeighbour) {
			startActivity(new Intent(getActivity(),
					SuperFriendsNearByActivity.class).putExtra(KEY_DATA, -2));
		} else if(v == frameLayoutSearch) {
			startActivity(new Intent(getActivity(), AddFriendActivity.class));
		} else if(v == mLinearLayoutNeighbourEntreprise) {
			startActivity(new Intent(getActivity(),
					EntrepriseNearByActivity.class).putExtra(KEY_DATA, -2));
			
		}
	}
	
}